/***************************************************************************
*   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
*   edgurgel@gmail.com                                                    *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 3 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "texture_material.h"

#include "common/image.h"
#include "common/color.h"

#include <iostream>

namespace Sirat {

namespace Material {
	
Texture_Material::Texture_Material(Common::Image* const _texture, const Common::Color& _reflectiveness) : Object_Material(Texture_Material_Type) , texture(_texture), reflectiveness(_reflectiveness) { }

Texture_Material::Texture_Material() : Object_Material(Texture_Material_Type) , texture(0) { } 

Texture_Material::Texture_Material(const Texture_Material& _texture_material) : Object_Material(_texture_material) {
 	texture = _texture_material.texture;
	reflectiveness = _texture_material.reflectiveness;
}

Texture_Material::~Texture_Material() { 
	if(texture)
		delete texture; 
}

Common::Color Texture_Material::get_texel(const double _u, const double _v) const {
	if(texture != 0) {
		if(texture->loaded()) texture->load_file(texture->get_filename().c_str()); //Ugly
		unsigned int x = _u*texture->get_width();
		unsigned int y = _v*texture->get_height();
		return texture->get_pixel(x,y);
	}
	else return Common::Color();
	
}

Common::Color Texture_Material::get_reflectiveness() const {
	return reflectiveness;
}
	
void Texture_Material::set_reflectiveness(const Common::Color& _reflectiveness) {
	reflectiveness = _reflectiveness;	
}

Texture_Material& Texture_Material::operator=(const Texture_Material& _texture_material) {
	if(this != &_texture_material) {
		Object_Material::operator=(_texture_material);
		texture = _texture_material.texture;
		reflectiveness = _texture_material.reflectiveness;
	}
	return *this;
}

}

}
